import * as vscode from 'vscode';
import { MavenDependencyAnalyzer } from './dependencyAnalyzer';
import { MavenRunner } from './mavenRunner';
import { TestRunner } from './testRunner';
import { MavenTerminal } from './mavenTerminal'; 
import { MavenModulesProvider } from './views/mavenModulesView';
import { DependencyAnalyzerProvider } from './views/dependencyAnalyzerView';
import { PomFileDecorator } from './pomFileDecorator';

/**
 * 初始化插件的各个功能模块
 */
export class Bootstrapper {
    private context: vscode.ExtensionContext;
    private dependencyAnalyzer: MavenDependencyAnalyzer;
    private mavenRunner: MavenRunner;
    private testRunner: TestRunner;
    private mavenTerminal: MavenTerminal;
    private mavenModulesProvider: MavenModulesProvider;
    private dependencyAnalyzerProvider: DependencyAnalyzerProvider;
    private pomFileDecorator: PomFileDecorator;
    
    constructor(context: vscode.ExtensionContext) {
        this.context = context;
        
        // 初始化各功能模块
        this.dependencyAnalyzer = new MavenDependencyAnalyzer(context);
        this.mavenRunner = new MavenRunner();
        this.testRunner = new TestRunner();
        this.mavenTerminal = new MavenTerminal();
        this.mavenModulesProvider = new MavenModulesProvider();
        this.dependencyAnalyzerProvider = new DependencyAnalyzerProvider();
        this.pomFileDecorator = new PomFileDecorator(context);
        
        // 注册视图
        this.registerViews();
        
        // 注册命令
        this.registerCommands();
        
        // 创建状态栏按钮
        this.createStatusBarItems();
    }
    
    /**
     * 注册视图提供者
     */
    private registerViews(): void {
        vscode.window.registerTreeDataProvider('maven-modules', this.mavenModulesProvider);
        vscode.window.registerTreeDataProvider('dependency-analyzer', this.dependencyAnalyzerProvider);
    }
    
    /**
     * 注册命令
     */
    private registerCommands(): void {
        // 依赖分析命令
        this.context.subscriptions.push(
            vscode.commands.registerCommand('maven-helper.analyzeDependencies', async () => {
                await this.dependencyAnalyzer.analyzeDependencies();
            })
        );
        
        // Maven目标命令
        this.context.subscriptions.push(
            vscode.commands.registerCommand('maven-helper.runMavenGoal', () => {
                this.mavenRunner.runMavenGoal(false);
            })
        );
        
        this.context.subscriptions.push(
            vscode.commands.registerCommand('maven-helper.runMavenGoalForRoot', () => {
                this.mavenRunner.runMavenGoal(true);
            })
        );
        
        // 终端命令
        this.context.subscriptions.push(
            vscode.commands.registerCommand('maven-helper.openTerminalAtModule', () => {
                this.mavenTerminal.openTerminalAtModule();
            })
        );
        
        // 测试命令
        this.context.subscriptions.push(
            vscode.commands.registerCommand('maven-helper.runCurrentTest', () => {
                this.testRunner.runCurrentTest(false);
            })
        );
        
        this.context.subscriptions.push(
            vscode.commands.registerCommand('maven-helper.debugCurrentTest', () => {
                this.testRunner.runCurrentTest(true);
            })
        );
        
        // 视图相关命令
        this.context.subscriptions.push(
            vscode.commands.registerCommand('maven-helper.refreshModules', () => {
                this.mavenModulesProvider.refresh();
            })
        );
        
        this.context.subscriptions.push(
            vscode.commands.registerCommand('maven-helper.refreshDependencies', () => {
                this.dependencyAnalyzerProvider.refresh();
            })
        );
        
        // 树视图项命令
        this.context.subscriptions.push(
            vscode.commands.registerCommand('maven-helper.openPomFile', (pomPath: string) => {
                vscode.workspace.openTextDocument(pomPath).then(document => {
                    vscode.window.showTextDocument(document);
                });
            })
        );
        
        this.context.subscriptions.push(
            vscode.commands.registerCommand('maven-helper.analyzePomDependencies', (pomPath: string) => {
                this.dependencyAnalyzerProvider.setPomFile(pomPath);
            })
        );
        
        this.context.subscriptions.push(
            vscode.commands.registerCommand('maven-helper.runGoalOnModule', (pomPath: string) => {
                this.runGoalOnSpecificModule(pomPath);
            })
        );
        
        this.context.subscriptions.push(
            vscode.commands.registerCommand('maven-helper.showConflictsOnly', () => {
                this.dependencyAnalyzerProvider.showConflictsOnly();
            })
        );
        
        this.context.subscriptions.push(
            vscode.commands.registerCommand('maven-helper.showAllDependencies', () => {
                this.dependencyAnalyzerProvider.showAllDependencies();
            })
        );
    }
    
    /**
     * 创建状态栏按钮
     */
    private createStatusBarItems(): void {
        const mavenRunItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left, 1);
        mavenRunItem.text = "$(play) Maven 运行";
        mavenRunItem.tooltip = "运行Maven目标";
        mavenRunItem.command = "maven-helper.runMavenGoal";
        mavenRunItem.show();
        this.context.subscriptions.push(mavenRunItem);
    }
    
    /**
     * 在特定模块上运行Maven目标
     */
    private async runGoalOnSpecificModule(pomPath: string): Promise<void> {
        const goal = await this.mavenRunner.selectMavenGoal();
        if (goal) {
            this.mavenRunner.runMavenGoalOnPath(pomPath, goal);
        }
    }
}
